import netaddr
from netaddr import IPRange


def ip_range2cidr(start_ip, end_ip):
    ip_cidr_list = []
    cidrs = IPRange(start_ip, end_ip).cidrs()
    for cidr in cidrs:
        ip_cidr_list.append(str(cidr))
    
    return ",".join(ip_cidr_list)


def ipv6_to_integer(ipv6):  # 问题：多冒号无法识别问题
    # 将IPv6地址分割成8组
    groups = ipv6.split(':')

    # 将每组转换为16位的二进制数
    binary_groups = [bin(int(group, 16))[2:].zfill(16) for group in groups]
    bin_list = []
    for group in groups:
        a = int(group, 16)
        b = bin(a)
        c = b[2:]
        d = c.zfill(16)
        print(f"group: {group} -> int a: {a} -> bin b: {b} -> substr c: {c} -> zfill d: {d}")
        bin_list.append(d)

    # 合并成128位的二进制数
    binary_string = ''.join(binary_groups)
    print(binary_string)

    # 转换为十进制数
    decimal_value = int(binary_string, 2)

    return decimal_value


## 根据子网掩码ip获取子网掩码长度


if __name__ == '__main__':
    # 示例IPv6地址
    ipv6_address = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
    result = ipv6_to_integer(ipv6_address)
    print(f"IPv6地址 {ipv6_address} 转换后的数值为 {result}")
    cdir = netaddr.iprange_to_cidrs('2001:1890:1412:100::', '2001:1890:1412:1ff:ffff:ffff:ffff:ffff')
    print(cdir)

